package com.c611.smartclassroom.service.Impl;

import com.c611.smartclassroom.mapper.UserMapper;
import com.c611.smartclassroom.model.User;
import com.c611.smartclassroom.model.UserExample;
import com.c611.smartclassroom.service.UserService;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;


import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
//yw 使用mybatis方式
    private UserMapper userMapper;
    /**
     * 管理员列表查询
     */
    @Cacheable(value = "user",key = "'queryUserByName::' + #name + '::' + #page + '::' + #limit",unless = "#result == null ")
    @Override
    public List<User> queryUserByName(String name,Integer page,Integer limit) {
        //增加分页
        RowBounds rowBounds = new RowBounds((page-1)*limit,limit);
        UserExample userExample = new UserExample();
        //增加查询条件
        userExample.createCriteria().andUserrealnameLike("%"+name+"%");
        //增加排序
        userExample.setOrderByClause("userAddTime desc");
        return userMapper.selectByExampleWithRowbounds(userExample,rowBounds); //yw pass
    }

    @Cacheable(value = "user",key = "'queryUserCountByName::' + #name",unless = "#result == null ")
    @Override
    public long queryUserCountByName(String name) {
        return userMapper.countByName(name);
    }
    /**
     * 教师列表查询
     */
    @Cacheable(value = "user",key = "'queryUser::' + #schoolId + '::' + #tBuildName + '::' + #name + '::' + #page + '::' + #limit",unless = "#result == null ")
    @Override
    public List<User> queryUser(String schoolId,String tBuildName,String name,Integer page,Integer limit) {
        page = (page-1)*limit;
        return userMapper.selectUserList(schoolId,tBuildName,name,page,limit);
    }

    @Cacheable(value = "user",key = "'queryUserCount::' + #schoolId + '::' + #tBuildName + '::' + #name ",unless = "#result == null ")
    @Override
    public long queryUserCount(String schoolId,String tBuildName,String name) {
        return userMapper.queryUserCount(schoolId,tBuildName,name);
    }

    @CacheEvict(value = "user",allEntries = true)
    @Override
    public int addUser(User user){
        user.setUseraddtime(new Date());
        user.setUserpwd(DigestUtils.md5DigestAsHex(("jgsu" + user.getUserpwd() + "c611").getBytes()));
        return userMapper.insertSelective(user); //yw pass
    }

    @CacheEvict(value = "user",allEntries = true)
    @Override
    public int saveUser(User user){
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andUseridEqualTo(user.getUserid().toString());
        if(user.getUserpwd() != null && !"".equals(user.getUserpwd()))
            user.setUserpwd(DigestUtils.md5DigestAsHex(("jgsu" + user.getUserpwd() + "c611").getBytes()));
        return userMapper.updateByExampleSelective(user,userExample); //yw pass
    }

    @CacheEvict(value = "user",allEntries = true)
    @Override
    public int delUser(Integer userId){
        return userMapper.deleteByPrimaryKey(userId.toString()); //yw pass
        //return userMapper.deleteByUpdateDelFlag(userId); //yw 另一种更复杂的方法
    }
}
